package com.briup.day19;

import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author ThinkBook
 */
public class Test03_Comparator {
    //匿名内部类对象方式实现比较器排序
    public static void main(String[] args) {
        Comparator<Teacher> comp = new Comparator<Teacher>() {
            //1.按照name升序
            //2.name相同按照balance降序
            //3.要求重复的元素也能放入
            @Override
            public int compare(Teacher o1, Teacher o2) {
                // 1.按照name升序
                int r = o1.getName().compareTo(o2.getName());
                if(r == 0) {
                    // 2.如果name相同则按照balance降序
                    if(o2.getBalance() == o1.getBalance())
                        r = 0;
                    else if(o2.getBalance() > o1.getBalance())
                        r = 1;
                    else
                        r = -1;
                }

                // 保证 重复的元素 也能成功放入
                return (r == 0) ? 1 : r;
            }
        };
        //2.实例化TreeSet（额外指定比较算法)
        Set<Teacher> set = new TreeSet<>(comp);
//        Set<Teacher> set = new TreeSet<>();

        set.add(new Teacher("Tom",2000.5));
        set.add(new Teacher("Jack",2320.5));
        set.add(new Teacher("Jack",3250.5));
        set.add(new Teacher("Rose",2020.5));
        set.add(new Teacher("Lucy",2000.5));

        Iterator<Teacher> it = set.iterator();
        while(it.hasNext()) {
            System.out.println(it.next());
        }
    }
    //定义实现类去实现比较器排序
    public static void main01(String[] args) {
        //1.实例化比较器对象
        Comparator<Teacher> comp = new TeaComp();

        //2.实例化TreeSet（额外指定比较算法)
        Set<Teacher> set = new TreeSet<>(comp);

        set.add(new Teacher("Tom",2000.5));
        set.add(new Teacher("Jack",2320.5));
        set.add(new Teacher("Rose",2020.5));
        set.add(new Teacher("Lucy",2000.5));

        Iterator<Teacher> it = set.iterator();
        while(it.hasNext()) {
            System.out.println(it.next());


        }
    }
}
//比较器排序：定义比较器实现类
class TeaComp implements Comparator<Teacher> {
    /*
        升序：o1 - o2
        降序: o2 - o2
     */
    @Override
    public int compare(Teacher o1, Teacher o2) {
        //先按照工资降序排列，如果工资相同则按照姓名升序
        // int = double - double;    89.9  -  89.5  == 0.4
        int r;
        if(o2.getBalance() == o1.getBalance()) {
            r = 0;
        }else if(o2.getBalance() > o1.getBalance()) {
            r = 1;
        }else {
            r = -1;
        }

        //如果工资相同则按照姓名升序
        if(r == 0) {
            r = o1.getName().compareTo(o2.getName());
        }

        return r;
    }

    //equals() 无需重写
}

class Teacher {
    private String name;
    private double balance;

    public Teacher() {
    }
    public Teacher(String name, double balance) {
        this.name = name;
        this.balance = balance;
    }

    public String getName() {
        return name;
    }

    public double getBalance() {
        return balance;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "name='" + name + '\'' +
                ", balance=" + balance +
                '}';
    }
}
